home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / ip / ka9q / net_src.arc / netuser.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-05-08  |  3.2 KB  |  159 lines

  1. /* Miscellaneous format conversion subroutines */
  2.  
  3. #include <ctype.h>
  4. #include <stdio.h>
  5. #include "global.h"
  6. #include "netuser.h"
  7. #ifdef UNIX
  8. #include "unix.h"
  9. #endif /* UNIX */
  10.  
  11. int net_error;
  12.  
  13. #define LINELEN 256
  14.  
  15. /* Convert Internet address in ascii dotted-decimal format (44.0.0.1) to
  16.  * binary IP address
  17.  */
  18. int32
  19. aton(s)
  20. register char *s;
  21. {
  22.     int32 n;
  23.     int atoi();
  24.     register int i;
  25.  
  26.     n = 0;
  27.     for(i=24;i>=0;i -= 8){
  28.         n |= (int32)atoi(s) << i;
  29.         if((s = index(s,'.')) == NULLCHAR)
  30.         break;
  31.         s++;
  32.     }
  33.     return n;
  34. }
  35. /* Resolve a host name into an IP address. IP addresses in dotted-decimal
  36.  * notation are distinguished from domain names by enclosing them in
  37.  * brackets, e.g., [44.64.0.1]
  38.  */
  39. int32
  40. resolve(host)
  41. char *host;
  42. {
  43.     register char *cp,*cp1;
  44.     int i;
  45.     char hostent[LINELEN];
  46.     FILE *sfile;
  47.     static struct {
  48.         char *name;
  49.         int32 address;
  50.     } cache;
  51. #ifdef UNIX
  52.     char *p;
  53.     char hostfn[256];
  54.     char *getenv();
  55. #endif /* UNIX */
  56.  
  57.     if(*host == '['){
  58.         /* Brackets indicate IP address in dotted-decimal form */
  59.         return aton(host + 1);
  60.     }
  61.     if(cache.name != NULLCHAR && strcmp(cache.name,host) == 0)
  62.         return cache.address;
  63.  
  64.     /* Not a numerical IP address, search the host table */
  65.     if((sfile = fopen(hosts,"r")) == NULL){
  66.         return 0;
  67.     }
  68.     while (!feof(sfile)){
  69.         fgets(hostent,LINELEN,sfile);
  70.         rip(hostent);
  71.         cp = hostent;
  72.         if(*cp == '#' || !isdigit(*cp))
  73.             continue;    /* Comment or invalid line */
  74.         while(cp != NULLCHAR){
  75.             /* Skip white space */
  76.             while(*cp == ' ' || *cp == '\t')
  77.                 cp++;
  78.             if(*cp == '\0')
  79.                 break;
  80.             /* Look for next token, find length of this one */
  81.             if((cp1 = index(cp,'\t')) != NULLCHAR){
  82.                 i = cp1 - cp;
  83.             } else if((cp1 = index(cp,' ')) != NULLCHAR) {
  84.                 i = cp1 - cp;
  85.             } else {
  86.                 i = strlen(cp);
  87.             }
  88.             if(strlen(host) == i && strncasecmp(host,cp,i) == 0){
  89.                 /* Found it, put in cache */
  90.                 fclose(sfile);
  91.                 if(cache.name != NULLCHAR)
  92.                     free(cache.name);
  93.                 cache.name = malloc((unsigned)strlen(host)+1);
  94.                 strcpy(cache.name,host);
  95.                 cache.address = aton(hostent);
  96.                 return cache.address;
  97.             }
  98.             /* That one didn't match, try the next one */
  99.             cp = cp1;
  100.         }
  101.     }
  102.     /* No address found */
  103.     fclose(sfile);
  104.     return 0;
  105. }
  106.  
  107. /* Convert an internet address (in host byte order) to a dotted decimal ascii
  108.  * string, e.g., 255.255.255.255\0
  109.  */
  110. char *
  111. inet_ntoa(a)
  112. int32 a;
  113. {
  114.     static char buf[16];
  115.  
  116.     sprintf(buf,"%u.%u.%u.%u",
  117.         hibyte(hiword(a)),
  118.         lobyte(hiword(a)),
  119.         hibyte(loword(a)),
  120.         lobyte(loword(a)) );
  121.     return buf;
  122. }
  123. /* Convert a socket (address + port) to an ascii string of the form
  124.  * aaa.aaa.aaa.aaa:ppppp
  125.  */
  126. char *
  127. psocket(s)
  128. struct socket *s;
  129. {
  130.     static char buf[30];
  131.  
  132.     sprintf(buf,"%s:%u",inet_ntoa(s->address),s->port);
  133.     return buf;
  134. }
  135. /* Convert hex-ascii string to long integer */
  136. long
  137. htol(s)
  138. char *s;
  139. {
  140.     long ret;
  141.     char c;
  142.  
  143.     ret = 0;
  144.     while((c = *s++) != '\0'){
  145. #if    (!ATARI_ST && !LATTICE)    /* DG2KK "ik versta er heelemal niets van!" */
  146.         c &= 0x7f;
  147. #endif
  148.         if(c >= '0' && c <= '9')
  149.             ret = ret*16 + (c - '0');
  150.         else if(c >= 'a' && c <= 'f')
  151.             ret = ret*16 + (10 + c - 'a');
  152.         else if(c >= 'A' && c <= 'F')
  153.             ret = ret*16 + (10 + c - 'A');
  154.         else
  155.             break;
  156.     }
  157.     return ret;
  158. }
  159.